package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 监听专辑的上架
     * @param albumId
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true") ,
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true"),
            key = MqConst.ROUTING_ALBUM_UPPER
    ))
    public void upperAlbum(Long albumId, Channel channel, Message message){

        if (albumId != null){
            log.info("监听到了上架消息:{}",albumId);
            searchService.upperAlbum(albumId);
        }

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 监听专辑下架
     * @param albumId
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true") ,
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true"),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    public void lowerAlbum(Long albumId,Channel channel , Message message){

        if (albumId != null){
            log.info("监听到了下架消息:{}",albumId);
            searchService.lowerAlbum(albumId);
        }

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 监听修改es表中的统计数据
     *
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TRACK,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_ES_STAT_UPDATE,durable = "true"),
            key = MqConst.ROUTING_TRACK_STAT_UPDATE
    ))
    public void updateStatOnEs(TrackStatMqVo mqVo,Channel channel,Message message){


        try {

            //做幂等性处理
            if (mqVo != null){

                String key = RedisConstant.BUSINESS_PREFIX + "es:" + mqVo.getBusinessNo();

                Boolean flag = redisTemplate.opsForValue().setIfAbsent(key, "1", 5, TimeUnit.MINUTES);

                if (flag){

                    //执行业务逻辑
                    searchService.updateStatOnEs(mqVo);
                }

            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
